﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.Permission;
using Enterprise.Data.Permission;
namespace Enterprise.Service.Permission
{
    /// <summary>
    /// 权限值管理应用服务
    /// </summary>
    public class PermissionService
    {
        /// <summary>
        ///  权限值缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Permission";

        /// <summary>
        /// 权限值列表 返回所有权限值
        /// </summary>
        /// <returns></returns>
        public List<PermissionEntity> PermissionList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<PermissionEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                PermissionData data = new PermissionData();
                List<PermissionEntity> list = data.PermissionList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 权限值 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void PermissionExecute(PermissionEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_PERMISSION(PACTION,PKEY,PNAME) values('" + entity.PACTION + "'," + entity.PKEY + ",'" + entity.PNAME + "')";
                    break;
                case "Update":
                    //cmd = "Update SYS_PERMISSION set PKEY=" + entity.PKEY + " and PNAME='" + entity.PNAME + "' where PACTION=''" + entity.PACTION+"'';
                    break;
                case "Delete":
                    cmd = "Delete from SYS_PERMISSION where PKEY=" + entity.PKEY;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                PermissionExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 组织机构操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void PermissionExecuteBySql(string cmd)
        {
            PermissionData data = new PermissionData();
            data.PermissionExecute(cmd);
        }

    }
}
